import 'package:flutter/material.dart';


class StateManagementDemo extends StatefulWidget {
  StateManagementDemo({Key key}) : super(key: key);

  @override
  _StateManagementDemo createState() => _StateManagementDemo();
}

class _StateManagementDemo extends State<StateManagementDemo> {
  int count = 0;

  void _increaseCount(){
    setState(() {
      count += 1;
    });
    print('$count');
  }

  @override
  Widget build(BuildContext context) {
    return CounterProvider(
      count: count,
      increaseCount: _increaseCount,
      child: Scaffold(
        appBar: AppBar(
          title: Text('StateManagementDemo'),
          elevation: 0.0,
        ),
        body: CounterWrapper(),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: _increaseCount,
        ),
      ),
    );
  }
}

class CounterWrapper extends StatelessWidget {
  // final int count;
  // final VoidCallback increaseCount;
  // const CounterWrapper(this.count,this.increaseCount,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: ChipDemo()
      );
  }
}

class ChipDemo extends StatelessWidget {
  // final int count;
  // final VoidCallback increaseCount;
  // const ChipDemo(this.count,this.increaseCount,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ActionChip(
      label: Text('${CounterProvider.of(context).count}'),
      onPressed: CounterProvider.of(context).increaseCount,
    );
  }
}

class CounterProvider extends InheritedWidget {
  CounterProvider({Key key, this.count, this.increaseCount,this.child}) : super(key: key, child: child);

  final int count;
  final VoidCallback increaseCount; 
  final Widget child;

  static CounterProvider of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<CounterProvider>();
  }

  @override
  bool updateShouldNotify(CounterProvider oldWidget) {
    return true;
  }
}